package com.yisu.expressway.customService;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.reflect.TypeToken;
import com.yisu.expressway.R;
import com.yisu.expressway.activity.BaseActivity;
import com.yisu.expressway.customService.adapter.InfoTypeAdapter;
import com.yisu.expressway.customService.adapter.VoiceAssistantAdapter;
import com.yisu.expressway.customService.model.HotTopicItem;
import com.yisu.expressway.customService.model.ServiceModule;
import com.yisu.expressway.customService.model.ServiceModuleID;
import com.yisu.expressway.databinding.ActivityInformationBinding;
import com.yisu.expressway.network.ApiClientHelper;
import com.yisu.expressway.network.ApiResult;
import com.yisu.expressway.network.ApiSetting;
import com.yisu.expressway.ui.recyclerview.BaseRvAdapter;
import com.yisu.expressway.ui.recyclerview.DisableScrollGridLayoutManager;
import com.yisu.expressway.ui.recyclerview.DividerGridItemDecoration;
import com.yisu.expressway.ui.recyclerview.DividerGridItemDecorationBottom;
import com.yisu.expressway.ui.recyclerview.DividerListItemDecoration;
import com.yisu.expressway.ui.recyclerview.PagingRecyclerView;
import com.yisu.expressway.utils.ToastUtil;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

public class InformationActivity extends BaseActivity implements PagingRecyclerView.SampleActionListener{

    ActivityInformationBinding binding;
    private int PAGE_SIZE = 20;
    private ServiceModule[] InfoTypeImgArray = {new ServiceModule(ServiceModuleID.HIGHWAY_CONDITION),
            new ServiceModule(ServiceModuleID.NAVIGATION_QUERY),
            new ServiceModule(ServiceModuleID.HIGHWAY_FEE_RULE),
            new ServiceModule(ServiceModuleID.WEATHER_CONDITION),
            new ServiceModule(ServiceModuleID.SERVICE_DISTRICT_AREA),
            new ServiceModule(ServiceModuleID.HIGHWAY_ILLEAGAL),
            new ServiceModule(ServiceModuleID.FEE_STANDARD),
            new ServiceModule(ServiceModuleID.HIGWAY_RESCUE)};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_information);
        initView();
    }

    private void initView(){
        showLoading();

        setTitle(R.string.my_custome_service);
        binding.infoSortList.setLayoutManager(new DisableScrollGridLayoutManager(this,4));
        binding.infoSortList.addItemDecoration(new DividerGridItemDecorationBottom(this,5));
        binding.infoSortList.setAdapter(new InfoTypeAdapter(this,InfoTypeImgArray));

        binding.hotTopicList.addItemDecoration(new DividerListItemDecoration(this, LinearLayoutManager.VERTICAL));
        binding.hotTopicList.init(this,PAGE_SIZE);
    }

    public void click2Assistant(View view){
        VoiceAssistantActivity.launch(this, ServiceModuleID.IFLY_MODULE);
    }

    @Override
    public void fetchData(int start, int pageSize) {
        getTopicList(start);
    }

    @Override
    public BaseRvAdapter.SampleViewHolder createItemViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(this).inflate(R.layout.text_item_with_right_arrow, parent, false);
        return new TopicViewHolder(view);
    }

    @Override
    public void bindData(BaseRvAdapter.SampleViewHolder _holder, int position, Object _item) {
        final TopicViewHolder holder = (TopicViewHolder)_holder;
        final HotTopicItem item = (HotTopicItem)_item;
        if(item != null){
            holder.mTvContent.setText(item.title);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ShowTextContentActivity.launch(InformationActivity.this,item.title,item.content);
                }
            });
        }
    }

    public class TopicViewHolder  extends BaseRvAdapter.SampleViewHolder {

        @Bind(R.id.tv_content)
        TextView mTvContent;

        public TopicViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }

    private void getTopicList(int offset){
        int pageNumber = offset / PAGE_SIZE + 1;
        Map<String, Object> params = new HashMap();
        params.put("pageindex", pageNumber);
        params.put("pagesize",PAGE_SIZE);

        ApiClientHelper.postApi(ApiSetting.getTodayHottestTopicList(), new TypeToken<ArrayList<HotTopicItem>>() {
        }, new JSONObject(params), new Response.Listener<ApiResult<ArrayList<HotTopicItem>>>() {
            @Override
            public void onResponse(ApiResult<ArrayList<HotTopicItem>> result) {

                dismissLoading();

                if (!result.success) {
                    ToastUtil.displayLongToastMsg(InformationActivity.this, result.getMsg());
                    return;
                }

                binding.hotTopicList.dataFetchDone(result.getResult());

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                dismissLoading();
                ToastUtil.displayShortToastMsg(InformationActivity.this, volleyError.getMessage());
            }
        }, this);
    }
}
